package org.grow.ceto.controller;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.grow.ceto.dao.DealBillDao;
import org.grow.ceto.entity.*;
import org.grow.ceto.service.PlayerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 21-8-29
 */

@RestController
@RequestMapping("/ceto/dealbill")
public class DealController {

    @Autowired
    private DealBillDao dealBillDao;
    @Autowired
    private PlayerService playerService;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @PostMapping("/insert")
    public DealBill insert(@RequestBody DealBill dealBill) {
        dealBill.setDealBillId(0);
        dealBill.setDealState(DealState.willing);
        dealBill.setDealTime(LocalDateTime.now());
        dealBill.setVersion(0);
        dealBillDao.save(dealBill);
        GoldCoinExchangeDto goldCoinExchangeDto = new GoldCoinExchangeDto();
        BeanUtils.copyProperties(dealBill, goldCoinExchangeDto);
        goldCoinExchangeDto.setCoinQuantity(dealBill.getQuantity());

        try {
            Boolean r = playerService.goldCoinExchange(goldCoinExchangeDto);
            if (r) {
                dealBill.setDealState(DealState.succeeded);
            }
            return dealBill;
        } catch (Exception e) {
            dealBill.setDealState(DealState.failed);
            throw e;
        }

    }

    private QDealBill d = QDealBill.dealBill;
    @PostMapping("/query")
    public PageResult<DealBill> query(@RequestBody DealBillSearchForm dealBillSearchForm){

        Integer pageNum = dealBillSearchForm.getPageNum();
        Integer pageSize = dealBillSearchForm.getPageSize();
        Integer pageStart = (pageNum-1)* pageSize;

        DealBillQueryCondition qc = dealBillSearchForm.getQueryCondition();

        BooleanBuilder builder = new BooleanBuilder();


        /*
        *     private LocalDateTime dealTimeStart;

    private LocalDateTime dealTimeStop;

    private String dealerCode;*/

        if (StringUtils.hasText(qc.getDealerCode())){

            BooleanBuilder codeBuilder = new BooleanBuilder();
            codeBuilder.or(d.supplierCode.eq(qc.getDealerCode()));
            codeBuilder.or(d.consumerCode.like(qc.getDealerCode()));
            builder.and(codeBuilder);
        }

        if (qc.getDealTimeStart()!=null){

            builder.and(d.dealTime.after(qc.getDealTimeStart()));
        }
        if (qc.getDealTimeStop()!=null){

            builder.and(d.dealTime.before(qc.getDealTimeStop()));
        }



        QueryResults<DealBill> queryResults = jpaQueryFactory.selectFrom(d)
                .where(builder)
                .orderBy(d.dealBillId.desc())
                .offset(pageStart)
                .limit(pageSize)
                .fetchResults();

        PageResult<DealBill> dealBillPageResult = new PageResult<>();
        dealBillPageResult.setTotal(queryResults.getTotal());
        dealBillPageResult.setResults(queryResults.getResults());
        return dealBillPageResult;
    }

}
